﻿package cn.poco.pepsi.activity.search;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import cn.poco.pepsi.R;
import cn.poco.pepsi.adapter.SearchMainMoreDishAapt;
import cn.poco.pepsi.bean.Dish;
import cn.poco.pepsi.service.impl.SearchImpl;
import cn.poco.pepsi.util.Cons;

/**
 * 更多菜式列表
 * @author tanglx
 *
 */
public class SearchMainMoreDishActivity extends Activity {
	private final static String TAG="SearchMainMoreDishActivity";
	private LinearLayout progressLayout;
	private List<Dish> list;
	private ListView listView;
	private Handler tHandler;
	private String cityCode,cityName;
	
	
	private void initView(){
		progressLayout=(LinearLayout)findViewById(R.id.progressbar);
		listView=(ListView)findViewById(R.id.listview);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.search_main_more_dish);
		super.onCreate(savedInstanceState);
		initView();
		cityCode=getIntent().getExtras().getString(Cons.CITY_CODE);
		cityName=getIntent().getExtras().getString(Cons.CITY_NAME);
		HandlerThread htHandlerThread=new HandlerThread(TAG);
		htHandlerThread.start();
		tHandler=new Handler(htHandlerThread.getLooper());
		tHandler.post(runnable);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent=new Intent(SearchMainMoreDishActivity.this,SearchResultActivity.class);
				intent.putExtra(Cons.SEARCH_DISHID, list.get(position).getId());
				intent.putExtra(Cons.ISPEPSI_RES, "no");
				startActivity(intent);
			}
		});
	}
	
	Handler handler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				ListAdapter listAdapter=new SearchMainMoreDishAapt(SearchMainMoreDishActivity.this, list);
				listView.setAdapter(listAdapter);
				progressLayout.setVisibility(View.GONE);
				listView.setVisibility(View.VISIBLE);
				break;
			default:
				break;
			}
			
		}
		
	};
	
	Runnable runnable=new Runnable() {
		@Override
		public void run() {
			SearchImpl searchImpl=new SearchImpl(SearchMainMoreDishActivity.this);
			try {
				list=searchImpl.getDishs(cityCode, "dish", SearchMainMoreDishActivity.this);
			} catch (Exception e) {
				e.printStackTrace();
			}
			handler.sendEmptyMessage(1);
		}
	};
	
	
	
}








